

cc.Class({
    extends: cc.Component,

    properties: {
        //移动的边界范围.
        maxMoveRange: 250,
        //移动速度.
        moveSpeed:250,
        lightNode: cc.Node,
    },

    // LIFE-CYCLE CALLBACKS:

    onLoad () {
        this.floorNum = 0;
        this.physicsCollider = this.getComponent(cc.PhysicsBoxCollider);
        this.rigidBody = this.getComponent(cc.RigidBody);

        if(this.boardManager == null){
            this.boardManager = this.node.parent.getComponent('BoardManager');
        }

        this.game = cc.find('Canvas/Game').getComponent('Game');
    },

    init () {
        //设置板子的初始速度.
        this.speed = (3 - 2 * this.node.scaleX) * this.moveSpeed;
        var xSpeed = this.speed;
        if(this.floorNum == 1 && Global.isFirstIn){
            xSpeed = 0;
        }
        xSpeed = cc.random0To1() > 0.5 ? xSpeed : -xSpeed;
        this.rigidBody.linearVelocity = cc.v2(xSpeed,0);
        this.physicsCollider.enabled = false;
    },

    update (dt) {
        //板子的移动.
        if(this.node.getPositionX() + this.node.scaleX*this.node.width/2 > 540){
            //速度改变.
            this.rigidBody.linearVelocity = cc.v2(-this.speed,0);
            if(this.floorNum == Global.currentFloor){
                //角色的速度跟着变.
                this.game.player.setVelocity(cc.v2(-this.speed,0));
            }
        }
        if(this.node.getPositionX() - this.node.scaleX*this.node.width/2 < -540){
            this.rigidBody.linearVelocity = cc.v2(this.speed,0);
            if(this.floorNum == Global.currentFloor){
                //角色的速度跟着变.
                this.game.player.setVelocity(cc.v2(this.speed,0));
            }
        }
    },

    onCollisionEnter: function(other, self){
        //检测角色是否进入了区域范围.
        if(other.tag == 1 && !this.boardManager.isPlayerUp()){
            //只能跳过一层，直接第二层不可以碰撞.
            if(other.getComponent('Player').reachedFloor + 1 < this.floorNum){
                return;
            }
            //设置自身的sensor状态.
            this.physicsCollider.enabled = true;
        }
    },
    
    onCollisionExit: function(other, self){
        //检测角色是否进入了区域范围.
        if(other.tag == 1){
            //设置自身的sensor状态.
            this.physicsCollider.enabled = false;
        }
    },

    //设置板子层数.
    setFloor(num){
        this.floorNum = num;
    },

    //获取中间判定点的宽度*(缩放倍数).
    getLightNodeWidth(){
        var self = this;
        return self.lightNode.width * self.node.scaleX;
    },

    //获取光中心点在世界坐标系的位置.
    getLightNodePosition(){
        var self = this;
        return self.node.getPositionX() + self.lightNode.getPositionX()*self.node.scaleX;
    },

    //获取表面层的位置.
    getSurfacePosition(){
        return this.node.height / 2;
    },

    isInCenter(playerPosX){
        var self = this;
        var selfCenter = self.node.x + self.lightNode.x;
        var width = self.lightNode.width * self.node.scaleX;
        return playerPosX <= selfCenter + width/2 && playerPosX >= selfCenter - width / 2;
    },
});
